package cn.tedu.egweibo0416.controller;

import cn.tedu.egweibo0416.base.response.JsonResult;
import cn.tedu.egweibo0416.mapper.CommentMapper;
import cn.tedu.egweibo0416.pojo.dto.CommentAddParam;
import cn.tedu.egweibo0416.pojo.entity.Comment;
import cn.tedu.egweibo0416.pojo.vo.CommentVO;
import cn.tedu.egweibo0416.pojo.vo.UserVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/v1/comment/")
@Api(tags = "03.评论功能模块")
public class CommentController {
    @Autowired
    CommentMapper commnetMapper;
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("insert")
    @ApiOperation(value="发表评论")
    @ApiOperationSupport(order=10)
    public JsonResult insert(@RequestBody CommentAddParam commentAddParam, @ApiIgnore HttpSession session){
        UserVO userVO= (UserVO)session.getAttribute("user");
        Comment comment=new Comment();
        BeanUtils.copyProperties(commentAddParam,comment);
        comment.setUserId(userVO.getId());
        comment.setCreateTime(new Date());
        commnetMapper.insertComment(comment);
        return JsonResult.ok();
    }
    @GetMapping("selectByWeiboId")
    @ApiOperation(value="微博评论列表")
    @ApiOperationSupport(order=20)
    public JsonResult selectByWeiboId(Long Id){
        List<CommentVO> commentVOs = commentMapper.selectByWeiboId(Id);
        return JsonResult.ok(commentVOs);
    }
}
